package com.zwh.controller;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zwh.entity.Album;
import com.zwh.entity.Chapter;
import com.zwh.entity.PageDTO;
import com.zwh.service.AlbumService;

@Controller
@RequestMapping("album")
public class AlbumController {
	@Autowired
	private AlbumService albumService;
	@RequestMapping("queryAlbum")
	@ResponseBody
	public PageDTO<Album> queryAlbum(int page,int rows){
		return albumService.selectAlbum(page, rows);
	}
	@RequestMapping("queryAlbumAll")
	@ResponseBody
	public List<Album> queryAlbumAll(){
		return albumService.selectAlbumAll();
	}
	
	
	@RequestMapping("addAlbum")
	@ResponseBody
	public String addAlbum(Album album,MultipartFile file,HttpSession session) throws IllegalStateException, IOException{
		String filename = file.getOriginalFilename();
		//String realPath = session.getServletContext().getRealPath("image/audioCollection");
		filename =new Date().getTime() + filename;
		File file2 = new File(session.getServletContext().getRealPath("image/audioCollection")+"/"+filename);
		
		album.setImg_path("image/audioCollection/"+filename);
		//System.out.println(album);
		albumService.addAlbum(album);
		
		file.transferTo(file2);
		
		
		return "";
	}
	

	@RequestMapping("addChapter")
	@ResponseBody
	public String addChapter(Chapter chapter,MultipartFile music,HttpSession session) throws IllegalStateException, IOException{
		String filename = music.getOriginalFilename();
		String realPath = session.getServletContext().getRealPath("music");
		filename =new Date().getTime() + filename;
		String path =realPath+"/"+filename;
		File file2 = new File(path);
		music.transferTo(file2);
		
		long size = music.getSize();//取大小
		
		String sizes =String.format("%.2f",(double)size/1024/1024)+"MB";//保留两位小并转换成字符串
		//获取时长
		Encoder encoder = new Encoder();
		String length = "";
		try {
			MultimediaInfo m = encoder.getInfo(file2);
			long ls = m.getDuration()/1000;
			int hour = (int) (ls/3600);
			int minute = (int) (ls%3600)/60;
			int second = (int) (ls-hour*3600-minute*60);
			length = hour+"时"+minute+"分"+second+"秒";
		} catch (Exception e) {
			e.printStackTrace();
		}
		//为实体类参数赋值
		chapter.setTitle(filename);
		chapter.setDuration(length);
		chapter.setSize(sizes);
		chapter.setUrl("music/"+filename);
		albumService.addChapter(chapter);
		
		
		return "";
	}
	
	@RequestMapping("downloadChapter")
	@ResponseBody
	public void downloadChapter(String title,HttpSession session,HttpServletResponse response) throws IllegalStateException, IOException{
		String realPath = session.getServletContext().getRealPath("music");
		String path =realPath+"/"+title;
		File file = new File(path);
		byte[] b = FileUtils.readFileToByteArray(file);
		response.setHeader("content-disposition","attachment;filename="+URLEncoder.encode(title, "UTF-8"));
		ServletOutputStream out = response.getOutputStream();
		out.write(b);
		if(out!=null) out.flush();
		if(out!=null) out.close();;
	}
	
	@RequestMapping("inlineChapter")
	public void inlineChapter(String title,HttpSession session,HttpServletResponse response) throws IllegalStateException, IOException{
		String realPath = session.getServletContext().getRealPath("music");
		String path =realPath+"/"+title;
		File file = new File(path);
		byte[] b = FileUtils.readFileToByteArray(file);
		response.setHeader("content-disposition","inline;filename="+URLEncoder.encode(title, "UTF-8"));
		ServletOutputStream out = response.getOutputStream();
		out.write(b);
		if(out!=null) out.flush();
		if(out!=null) out.close();;
	}
}
